//
//  EMMNotSignedBackViewController.m
//  EMMApp
//
//  Created by Mac on 16/7/8.
//  Copyright © 2016年 YiXun. All rights reserved.
//

#import "EMMNotSignedBackViewController.h"
#import "MenuLitsView.h"
#import "EMMAnalysisCell.h"
#import "ZFChart.h"
#import "GetPaymentNotSignedListApi.h"
@interface EMMNotSignedBackViewController ()<UITableViewDelegate,UITableViewDataSource> {
    
}
@property (nonatomic, strong) UITableView *tableView;

@property (strong, nonatomic) UIView *timerView;

@property (strong, nonatomic) ZFBarChart * barChart;

@property (nonatomic, strong) UILabel *jlLabel;
@property (nonatomic, strong) GetPaymentNotSignedList *getPaymentNotSignedList;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) NSMutableArray *dataNameArr;
@property (nonatomic, strong) NSMutableArray *dataNumArr;

@end

@implementation EMMNotSignedBackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatNavigationController];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, EMM_HEIGHT-SafeAreaTopHeight) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - 导航

- (void)creatNavigationController {
    [self updateNavBar];
    self.navigationItem.title = @"已签约未回款";
    [self configLeftBtn];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    _dataArr = [[NSMutableArray alloc] init];
    _dataNameArr = [[NSMutableArray alloc] init];
    _dataNumArr = [[NSMutableArray alloc] init];
    [self postGetPaymentNotSignedListApi];
}


- (void)postGetPaymentNotSignedListApi {
    [self showHudInWindow:@"正在加载请稍等..."];

    [_dataArr removeAllObjects];
    [_dataNumArr removeAllObjects];
    [_dataNameArr removeAllObjects];
    
    GetPaymentNotSignedListApi *api = [[GetPaymentNotSignedListApi alloc] initWith];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        GetPaymentNotSignedListApi *result = (GetPaymentNotSignedListApi *)request;
        if (result.isSuccess) {
            GetPaymentNotSignedList *get = result.resultData;
            _getPaymentNotSignedList = get;
            for (PaymentModeList *list in get.paymentModeList) {
                [_dataArr addObject:list];
                [_dataNameArr addObject:list.paymentModeName];
                [_dataNumArr addObject:list.paymentModeAmount];
            }
            [self creatTimerShowUI];
        } else {
            [self showToast:result.resultData.errorMsg];
            /*
             提示语：账号权限发生变动，请重新登录
             code随机18位唯一码：108E65843mYuwqb01X
             //YUplf0wyU5vPVssLSG 对应的异地登录退出登录
             */
            if ([result.resultData.errorCode isEqualToString:@"YUplf0wyU5vPVssLSG"] || [result.resultData.errorCode isEqualToString:@"108E65843mYuwqb01X"]) {
                NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
                NSDictionary *dictionary = [defs dictionaryRepresentation];
                for(NSString *key in [dictionary allKeys]){
                    if ([key isEqualToString:@"DeviceToken123"] ||
                        [key isEqualToString:@"loginText"] ||
                        [key isEqualToString:@"provinceCityDistrict"]) {
                    }else {
                        [defs removeObjectForKey:key];
                        [defs synchronize];
                    }
                }
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:result.resultData.errorMsg preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                    [appInstance showLogin];
                }];
                [alertController addAction:cancelAction];
                [self presentViewController:alertController animated:YES completion:nil];
            }
        }
        [_tableView reloadData];
        [self hideHud];
    } failure:^(YTKBaseRequest *request) {
        [self showToast:@"连接服务器失败，请检查网络！"];
        [self hideHud];
    }];
}


#pragma mark - UITableView代理
//返回几个表头
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

//每一个表头下返回几行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 300;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"EMMAnalysisCellId";
    EMMAnalysisCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"EMMAnalysisCell" owner:self options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
    [cell addSubview:self.timerView];

    if (_dataArr.count > 0) {
        [cell initWithXNumArr:_dataNameArr oneNumArr:_dataNumArr indexPath:indexPath];
    }else {
        UILabel *jlLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 300/2, EMM_WIDTH, 20)];
        jlLabel.text = @"当前没有记录";
        jlLabel.textAlignment = NSTextAlignmentCenter;
        jlLabel.font = [UIFont systemFontOfSize:15];
        jlLabel.textColor = [UIColor colorFromHexCode:@"#1A1A1A"];
        [cell addSubview:jlLabel];
    }
    return cell;
}


- (void)creatTimerShowUI {
    self.timerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, 40)];
    self.timerView.backgroundColor = [UIColor colorFromHexCode:@"#fff5e9"];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
    label.text = @"总额";
    label.font = [UIFont systemFontOfSize:15];
    label.textColor = [UIColor colorFromHexCode:@"#1A1A1A"];
    [self.timerView addSubview:label];
    
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(EMM_WIDTH-100-10, 10, 100, 20)];
    if ([NSString isStringEmpty2:_getPaymentNotSignedList.totalAmount] == NO) {
        label1.text = [NSString stringWithFormat:@"%@万",_getPaymentNotSignedList.totalAmount];
    }else {
        label1.text = [NSString stringWithFormat:@"0万"];
    }
    label1.font = [UIFont systemFontOfSize:14];
    label1.textAlignment = NSTextAlignmentRight;
    [self.timerView addSubview:label1];
    [_tableView reloadData];
}
@end
